import * as ACTION from '../../actions/fixup-work/dataAction';
import assign from 'lodash/object/assign';

/**
 * 数据视图默认状态
 * @type {Object}
 */
export const DEFAULT_DATA_STATE = {
	// 是否正在请求图表数据
	isChartFetching: false,
	// 类别(x轴刻度数据)
	categories: [],
	// 组(折线数据)
	series: [],
	// 当前视图
	view: -1,
};

/**
 * 数据视图Reducer
 * @param  {Object} state  当前状态
 * @param  {Object} action 当前Action
 * @return {Object}        新状态
 */
export function data(state = DEFAULT_DATA_STATE, action = {}) {
	let { type } = action;
	
	switch(type) {
		// 清除数据
		case ACTION.CLEAR_DATA:
			return assign({}, state, { categories: [], series: [] });
		// 获取数据
		case ACTION.GET_DATA_REQUEST: 
			let { view } = action.payload;
			return assign({}, state, { view, isChartFetching: true });
		case ACTION.GET_DATA_SUCCESS:
			let { data } = action.payload;
			let { categories, series } = data;
			return assign({}, state, { isChartFetching: false, categories, series });
		case ACTION.GET_DATA_FAILURE:
			return assign({}, state, { isChartFetching: false });
		default:
			return state;
	}
}